package com.dengbw.systemtransfer.service;

import com.dengbw.systemtransfer.mapper.AdministrativeAreaMapper;
import com.dengbw.systemtransfer.model.AdministrativeArea;
import com.dengbw.systemtransfer.model.exceptions.MyRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author dengbw
 * @date 2021/11/11 2:28 下午
 */
@Slf4j
@Service
public class AdminAreaService {

    AdministrativeAreaMapper administrativeAreaMapper;

    @Autowired
    public AdminAreaService(AdministrativeAreaMapper administrativeAreaMapper) {
        this.administrativeAreaMapper = administrativeAreaMapper;
    }

    public String getAreaId(String shi, String quxian, String zhen, String cun) {
        AdministrativeArea shiArea = getByName(shi, "0", "210000000000");
        if (shiArea == null)
            shiArea = getByLike(shi, "0", "210000000000");
        if (shiArea == null) {
            log.error("未找到市：" + shi);
            return "";
        }

        AdministrativeArea quArea = getByName(quxian, "1", shiArea.getAreaId());
        if (quArea == null)
            quArea = getByLike(quxian, "1", shiArea.getAreaId());
        if (quArea == null) {
            log.error("未找到区：" + quxian);
            return "";
        }

        AdministrativeArea zhenArea = getByName(zhen, "2", quArea.getAreaId());
        if (zhenArea == null)
            zhenArea = getByLike(zhen, "2", quArea.getAreaId());
        if (zhenArea == null) {
            log.info("未找到镇：" + zhen);
            return "";
        }
        AdministrativeArea finalZhenArea = zhenArea;
        AdministrativeArea cunArea = getByLike(cun, "3", finalZhenArea.getAreaId());
        if (cunArea == null) {
            log.info("未找到村：" + cun);
            return "";
        }
        return cunArea.getAreaId();
    }

    private AdministrativeArea getByLike(String name, String level, String parentId) {
        List<AdministrativeArea> areaList = administrativeAreaMapper.queryList(v ->
                v.like("area_name", buildNameFilter(name))
                        .eq("level", level)
                        .eq("parent_id", parentId));
        if (areaList.isEmpty())
            return null;
        if (areaList.size() > 1)
            throw new MyRuntimeException("找到的数据不唯一：" + areaList);
        return areaList.get(0);
    }

    private String buildNameFilter(String name) {
        char[] nameChars = name.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (char c : nameChars) {
            builder.append("%").append(c);
        }
        return builder + "%";
    }

    private AdministrativeArea getByName(String name, String level, String parentId) {
        List<AdministrativeArea> areaList = administrativeAreaMapper.queryList(v ->
                v.eq("area_name", name).eq("level", level).eq("parent_id", parentId));
        if (areaList.isEmpty())
            return null;
        if (areaList.size() > 1)
            throw new MyRuntimeException("找到的数据不唯一：" + areaList);
        return areaList.get(0);
    }
}
